package com.github.ecbp.user.service.controller.resource;

import com.github.ecbp.common.constant.ConstantCode;
import com.github.ecbp.common.data.utils.TransformUtils;
import com.github.ecbp.common.req.BaseIdDTO;
import com.github.ecbp.common.req.BaseIdListDTO;
import com.github.ecbp.common.security.vo.DefaultUserPermission;
import com.github.ecbp.user.service.api.PermissionApi;
import com.github.ecbp.user.service.entity.TMenu;
import com.github.ecbp.user.service.entity.TPermission;
import com.github.ecbp.user.service.response.AdminWithPermissionRes;
import com.github.ecbp.user.service.response.MenuRes;
import com.github.ecbp.user.service.response.PermissionRes;
import com.github.ecbp.user.service.service.MenuService;
import com.github.ecbp.user.service.service.PermissionMenuRelationService;
import com.github.ecbp.user.service.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 后台用户表 前端控制器
 * </p>
 *
 * @author zj2626
 * @since 2020-03-18
 */
@RestController
public class PermissionApiController implements PermissionApi {
    @Autowired
    private PermissionService permissionService;
    @Autowired
    private PermissionMenuRelationService permissionMenuRelationService;
    @Autowired
    private MenuService menuService;

    @Override
    public List<PermissionRes> getPermissionVoByMenuId(@Validated @RequestBody BaseIdListDTO param) {
        return permissionMenuRelationService.getPermissionVoByMenuId(new HashSet<>(param.getIds()));
    }

    @Override
    public List<PermissionRes> getPermissionVoById(BaseIdListDTO param) {
        List<TPermission> permissions = permissionService.getByIds(param);
        permissions = permissions.stream().filter(permission -> ConstantCode.TRUE == permission.getStatus()).collect(Collectors.toList());
        return TransformUtils.listCopy(permissions, PermissionRes.class);
    }

    @Override
    public List<MenuRes> getMenuVoByPermissionId(@Validated @RequestBody BaseIdListDTO param) {
        return permissionMenuRelationService.getMenuVoByPermissionId(new HashSet<>(param.getIds()));
    }

    @Override
    public List<MenuRes> getMenuVoById(@Validated @RequestBody BaseIdListDTO param) {
        List<TMenu> menus = menuService.getByIds(param);
        menus = menus.stream().filter(menu -> ConstantCode.TRUE == menu.getStatus()).collect(Collectors.toList());
        return TransformUtils.listCopy(menus, MenuRes.class);
    }

    @Override
    public List<MenuRes> getAllMenuVo() {
        List<TMenu> menus = menuService.list();
        menus = menus.stream().filter(menu -> ConstantCode.TRUE == menu.getStatus()).collect(Collectors.toList());
        return TransformUtils.listCopy(menus, MenuRes.class);
    }

    @Override
    public AdminWithPermissionRes getPermissionList(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        AdminWithPermissionRes adminWithPermissionRes = new AdminWithPermissionRes();

        DefaultUserPermission userPermission = new DefaultUserPermission();
        List<TPermission> permissions = permissionService.getPermissionList(param.getId(), userPermission);
        final List<PermissionRes> permissionRes = TransformUtils.listCopy(permissions, PermissionRes.class);
        adminWithPermissionRes.setPermissionResList(CollectionUtils.isEmpty(permissionRes) ? Collections.emptyList() : permissionRes);
        adminWithPermissionRes.setRoles(userPermission.getRoles());
        adminWithPermissionRes.setMenus(userPermission.getMenus());
        adminWithPermissionRes.setPermissions(userPermission.getPermissions());
        return adminWithPermissionRes;
    }


    @Override
    public List<MenuRes> getMenuList(@Validated(BaseIdDTO.NotNullId.class) @RequestBody BaseIdDTO param) {
        final List<TMenu> menus = menuService.getMenuList(param.getId());
        return TransformUtils.listCopy(menus, MenuRes.class);
    }
}
